﻿<template>
    <div class="esChannel-container">
        <el-dialog v-model="state.isShowDialog" draggable width="769px" @close="closeDialog">
            <template #header>
                <div style="color: #fff">
                    <el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle">
                        <ele-Edit/>
                    </el-icon>
                    <span> {{ props.title }} </span>
                </div>
            </template>
            <el-form :model="state.ruleForm" ref="ruleFormRef" size="default" label-width="80px" :rules="rules">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="名称" prop="name">
                            <el-input v-model="state.ruleForm.name" placeholder="请输入渠道名称" class="w100"
                                      clearable/>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="简称" prop="alias">
                            <el-input v-model="state.ruleForm.alias" placeholder="请输入渠道简称" class="w100"
                                      clearable/>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="编码" prop="code">
                            <el-input v-model="state.ruleForm.code" placeholder="请输入渠道简称首字母" class="w100"
                                      clearable/>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="状态">
                            <el-radio-group v-model="state.ruleForm.status" class="ml-4">
                                <el-radio
                                    v-for="item in optionDict.YesNoDict"
                                    :key="item.value"
                                    :label="item.value"
                                >
                                    {{ item.label }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
<!--                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">-->
<!--                        <el-form-item label="用户名" prop="userName">-->
<!--                            <el-input v-model="state.ruleForm.userName" placeholder="请输入渠道登录用户名" class="w100"-->
<!--                                      clearable/>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->
<!--                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">-->
<!--                        <el-form-item label="登录密码" prop="passWord">-->
<!--                            <el-input v-model="state.ruleForm.passWord" placeholder="请输入渠道登录密码" class="w100"-->
<!--                                      clearable/>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="类型" prop="channelType">
                            <el-select v-model="state.ruleForm.channelType" placeholder="请选择" class="w100">
                                <el-option v-for='item in optionDict.channelTypeDict' :key='item.value'
                                           :label='item.label' :value='item.value'/>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="备注">
                            <el-input v-model="state.ruleForm.remark" placeholder="请输入备注内容" clearable
                                      type="textarea"
                                      maxlength="255" :autosize="{ minRows: 2 }"/>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel" size="default">取 消</el-button>
					<el-button type="primary" @click="submit" size="default">确 定</el-button>
				</span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="editEsChannel">
import {reactive, ref} from 'vue';
import {ElMessage} from 'element-plus';
import type {FormRules} from "element-plus";
import mittBus from '/@/utils/mitt';

import {EsChannel} from '/@/api/models';
import SysEsChannelApi from '/@/api/exocytosis/channel';

import {optionDict} from "/@/const/dict";

const props = defineProps({
    title: String,
});

const ruleFormRef = ref();
const state = reactive({
    isShowDialog: false,
    ruleForm: {} as EsChannel,
});

// 删除非必填规则
const rules = ref<FormRules>({
    name: [{required: true, message: '请输入渠道名称！', trigger: 'blur',},],
    code: [{required: true, message: '请输入渠道名称汉字首字母（不超过4位长度）！', trigger: 'blur',},],
    status: [{required: true, message: '请输入渠道状态！', trigger: 'blur',},],
});

// 打开弹窗
const openDialog = (row: EsChannel, conDition: any) => {
    if (conDition == 'add') {
        state.ruleForm = row;
        state.isShowDialog = true;
    } else {
        state.ruleForm = row;
        state.isShowDialog = true;
    }
};

// 关闭弹窗
const closeDialog = () => {
    mittBus.emit('submitRefresh');
    state.isShowDialog = false;
};

// 取消
const cancel = () => {
    state.isShowDialog = false;
};

// 提交
const submit = () => {
    ruleFormRef.value.validate(async (valid: boolean, fields?: any) => {
        if (valid) {
            let res;
            if (state.ruleForm.id) {
                res = await SysEsChannelApi().editPost(state.ruleForm);
            } else {
                res = await SysEsChannelApi().addPost(state.ruleForm);
            }
            if (res.data.resCode != '0000')
                ElMessage.error(res.data.resMessage);
            else {
                ElMessage.success("操作成功");
                closeDialog();
            }
        } else {
            ElMessage({
                message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
                type: "error",
            });
        }
    });
};

// 导出对象 将属性或者函数暴露给父组件
defineExpose({openDialog});
</script>
